﻿using System.Linq;
using BlueDot.Data.Core;
using Microsoft.Synchronization.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Syncronization.Utils;

namespace SmartDevice.Syncronization.UnitTests
{
    /// <summary>
    /// Summary description for SyncSchemUtilsFixture
    /// </summary>
    [TestClass]
    public class SyncSchemUtilsFixture
    {
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #region Tests

        [TestMethod]
        public void CanAddSchemaBuilderToSyncSchema()
        {
            var schemaBuilder1 = new TestSchemaBuilder {Name = "test1"};
            schemaBuilder1.AddColumn(
                new DataStoreColumnSchema
                    {
                        Name = "ID",
                        IsUniqueKey = true,
                        IsRequired = true,
                        ClrDataType = typeof (int)
                    });

            schemaBuilder1.AddColumn(
                new DataStoreColumnSchema
                    {
                        Name = "Col1",
                        IsUniqueKey = false,
                        IsRequired = true,
                        ClrDataType = typeof (string),
                        MaxLength = 20
                    });

            schemaBuilder1.AddColumn(
                new DataStoreColumnSchema
                    {
                        Name = "Col2",
                        IsUniqueKey = false,
                        IsRequired = false,
                        ClrDataType = typeof (string),
                        MaxLength = 10
                    });

            var schema = new SyncSchema();

            SyncSchemaTable syncTable = SyncSchemaUtils.CreateSyncSchemTable(schemaBuilder1, schema);

            Assert.IsTrue(schema.Tables.Contains(syncTable.TableName));

            Assert.AreEqual(schemaBuilder1.Columns.Count, syncTable.Columns.Count);

            AssertSyncTable(schemaBuilder1, syncTable);
        }

        #endregion

        #region Supporting Code

        public static void AssertSyncTable(ISchemaBuilder schemaBuilder1, SyncSchemaTable syncTable)
        {
            foreach (var item in schemaBuilder1.Columns)
            {
                DataStoreColumnSchema column = item.Value;

                Assert.IsTrue(syncTable.Columns.Contains(column.Name));
                SyncSchemaColumn syncColumn = syncTable.Columns[column.Name];
                Assert.AreEqual(column.Name, syncColumn.ColumnName);
                Assert.AreEqual(column.IsGeneratedByDataStore, syncColumn.AutoIncrement);
                Assert.AreEqual(!column.IsRequired, syncColumn.AllowNull);
                Assert.AreEqual(column.ClrDataType, syncColumn.DataType);

                if (column.IsUniqueKey)
                    Assert.IsTrue(syncTable.PrimaryKey.Contains(column.Name));
                else
                    Assert.IsFalse(syncTable.PrimaryKey.Contains(column.Name));
            }
        }

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }
    }
}